#include <QDebug>
#include <QSerialPortInfo>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "widgetconfigcomm.h"
#include "config.h"
#include <QDesktopServices>
#include <QListView>
#include <QDateTime>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    startInit();
    myCom = NULL;

    ui->qter->setText(tr("<a href=\"http://www.fzzygf.com\">基于Qter的QCom版本修改 www.fzzygf.com</a>"));
 #ifdef Q_OS_LINUX
    ui->portNameComboBox->addItem( "ttyUSB0");
    ui->portNameComboBox->addItem( "ttyUSB1");
    ui->portNameComboBox->addItem( "ttyUSB2");
    ui->portNameComboBox->addItem( "ttyUSB3");
    ui->portNameComboBox->addItem( "ttyS0");
    ui->portNameComboBox->addItem( "ttyS1");
    ui->portNameComboBox->addItem( "ttyS2");
    ui->portNameComboBox->addItem( "ttyS3");
    ui->portNameComboBox->addItem( "ttyS4");
    ui->portNameComboBox->addItem( "ttyS5");
    ui->portNameComboBox->addItem( "ttyS6");
#elif defined (Q_OS_WIN)
//    ui->portNameComboBox->addItem("COM0");
//    ui->portNameComboBox->addItem("COM1");
//    ui->portNameComboBox->addItem("COM2");
//    ui->portNameComboBox->addItem("COM3");
//    ui->portNameComboBox->addItem("COM4");
//    ui->portNameComboBox->addItem("COM5");
//    ui->portNameComboBox->addItem("COM6");
//    ui->portNameComboBox->addItem("COM7");
//    ui->portNameComboBox->addItem("COM8");
//    ui->portNameComboBox->addItem("COM9");
//    ui->portNameComboBox->addItem("COM10");
//    ui->portNameComboBox->addItem("COM11");
//    ui->portNameComboBox->addItem("COM12");
//    ui->portNameComboBox->addItem("COM13");
//    ui->portNameComboBox->addItem("COM14");
//    ui->portNameComboBox->addItem("COM15");
    ui->portNameComboBox->setView(new QListView);
    ui->baudRateComboBox->setView(new QListView);
    ui->dataBitsComboBox->setView(new QListView);
    ui->parityComboBox->setView(new QListView);
    ui->stopBitsComboBox->setView(new QListView);
    ui->chradioButton->setChecked(true);
    ui->chexButton->hide();

    m_curIndexId = 1;

    //查找可用的串口
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        ui->portNameComboBox->addItem(info.portName());
    }
#endif

    ui->statusBar->showMessage(tr("欢迎使用 串口调试助手, 基于QCom修改版!"));

    if (!WidgetConfigComm::LoadWidgetConfig(static_cast<QWidget*>(ui->tab),
        static_cast<QWidget*>(ui->tab)))
    {
        QMessageBox::warning(this, tr("提示"), tr("加载 扩展多条发送配置 失败!"));
    }

    if (!WidgetConfigComm::LoadWidgetConfig(static_cast<QWidget*>(ui->scrollAreaWidgetContents),
        static_cast<QWidget*>(ui->scrollAreaWidgetContents)))
    {
        QMessageBox::warning(this, tr("提示"), tr("加载 扩展多条发送配置 失败!"));
    }

    // 关联按钮信号槽
    for (size_t i = 1; i < 34; ++i)
    {
        QPushButton* btn = ui->tab->findChild<QPushButton*>(
            QString("pushBtnSendData%1").arg(QString::number(i)));
        connect(btn, SIGNAL(clicked()), this, SLOT(SendMultiData()));
    }
    // 关联文本信号槽
    for (size_t i = 1; i < 34; ++i)
    {
        QLineEdit* txt = ui->tab->findChild<QLineEdit*>(
            QString("txtSendData%1").arg(QString::number(i)));
        txt->installEventFilter(this);
    }

    ui->cbAutoForSend->setChecked(false);
    ui->sendAsHexcheckBox->setChecked(true);
    ui->cbMSHex->setChecked(true);
    m_pCircleSendTimer = new QTimer;
    connect(m_pCircleSendTimer,&QTimer::timeout, this, &MainWindow::onCircleSend);
}

MainWindow::~MainWindow()
{
	if (!WidgetConfigComm::SaveWidgetConfig(static_cast<const QWidget*>(ui->tab),
		static_cast<const QWidget*>(ui->tab)))
	{
        QMessageBox::warning(this, tr("提示"), tr("保存多条发送数据配置失败!"));
	}

	if (!WidgetConfigComm::SaveWidgetConfig(static_cast<const QWidget*>(ui->scrollAreaWidgetContents),
		static_cast<const QWidget*>(ui->scrollAreaWidgetContents)))
	{
        QMessageBox::warning(this, tr("提示"), tr("保存多条发送数据配置失败!"));
	}
	
	if (myCom != NULL)
	{
		if (myCom->isOpen())
		{
			myCom->close();
		}
		delete myCom;
	}
	
	delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
	QMainWindow::changeEvent(e);
	switch (e->type())
	{
	case QEvent::LanguageChange:
		ui->retranslateUi(this);
		break;
	default:
		break;
	}
}

void MainWindow::startInit()
{
	setActionsEnabled(false);
	ui->delayspinBox->setEnabled(false);
	ui->sendmsgBtn->setEnabled(false);
	ui->sendMsgLineEdit->setEnabled(false);
	ui->obocheckBox->setEnabled(false);
	ui->actionAdd->setEnabled(true);

    //初始化连续发送计时器计时间隔
    obotimerdly = OBO_TIMER_INTERVAL;

    //设置连续发送计时器
    obotimer = new QTimer(this);
    connect(obotimer, SIGNAL(timeout()), this, SLOT(sendMsg()));

}

void MainWindow::setActionsEnabled(bool status)
{
    ui->actionSave->setEnabled(status);
    ui->actionClose->setEnabled(status);
    ui->actionLoadfile->setEnabled(status);
    ui->actionCleanPort->setEnabled(status);
    ui->actionWriteToFile->setEnabled(status);
}

void MainWindow::setComboBoxEnabled(bool status)
{
    ui->portNameComboBox->setEnabled(status);
    ui->baudRateComboBox->setEnabled(status);
    ui->dataBitsComboBox->setEnabled(status);
    ui->parityComboBox->setEnabled(status);
    ui->stopBitsComboBox->setEnabled(status);
}

bool MainWindow::eventFilter(QObject* obj, QEvent* event)
{
	bool bRet = true;

	switch (event->type())
	{
	case QEvent::MouseButtonDblClick:
	{
		QString szObjName = obj->objectName();
		QString szTmp = szObjName;
		QString szSearch = "txtSendData";
		int nTxtID = szTmp.replace(szSearch, "").toInt();
		if (szObjName.end() != 
			std::search(szObjName.begin(), szObjName.end(), 
				szSearch.begin(), szSearch.end()))
		{
			// 弹出输入框
			bool ok;
			QString text = QInputDialog::getText(this, 
				QString(tr("请输入 %1 号的发送数据注释:")).arg(QString::number(nTxtID)),
				tr("注释:"), QLineEdit::Normal,
				QDir::home().dirName(), &ok);
			if (ok && !text.isEmpty())
			{
				// 保存注释
				QStringList keys, values;
				keys << QString("txtSendData%1Comment").arg(QString::number(nTxtID));
				values << text;
				saveConfig("Comment", keys, values);
			}
		}
	}
		break;
	case QEvent::MouseMove:
	{
		QString szObjName = obj->objectName();
		QString szTmp = szObjName;
		QString szSearch = "txtSendData";
		int nTxtID = szTmp.replace(szSearch, "").toInt();
		if (szObjName.end() !=
			std::search(szObjName.begin(), szObjName.end(),
				szSearch.begin(), szSearch.end()))
		{
			QString key, value;
			key = QString("txtSendData%1Comment").arg(QString::number(nTxtID));
			value = loadConfig("Comment", key);
			QLineEdit* txt = static_cast<QLineEdit*>(obj);
			txt->setStatusTip(value);
		}
	}
		break;
	case QEvent::MouseButtonRelease:
	{
		QString szObjName = obj->objectName();
		if (0 == QString::compare(szObjName, "lblLogoQter"))
		{
			QDesktopServices::openUrl(QUrl("http://www.fzzygf.org"));
		}
	}
		break;
	default:
		bRet = QObject::eventFilter(obj, event);
	}

	return bRet;
}

//打开串口
void MainWindow::on_actionOpen_triggered()
{
    QString portName = ui->portNameComboBox->currentText();   //获取串口名
#ifdef Q_OS_LINUX
    myCom = new QextSerialPort("/dev/" + portName);
#elif defined (Q_OS_WIN)
    myCom = new QextSerialPort(portName);
#endif
    connect(myCom, SIGNAL(readyRead()), this, SLOT(readMyCom()));

    //设置波特率
    myCom->setBaudRate((BaudRateType)ui->baudRateComboBox->currentText().toInt());

    //设置数据位
    myCom->setDataBits((DataBitsType)ui->dataBitsComboBox->currentText().toInt());

    //设置校验
    switch(ui->parityComboBox->currentIndex()){
    case 0:
         myCom->setParity(PAR_NONE);
         break;
    case 1:
        myCom->setParity(PAR_ODD);
        break;
    case 2:
        myCom->setParity(PAR_EVEN);
        break;
    default:
        myCom->setParity(PAR_NONE);
        qDebug("set to default : PAR_NONE");
        break;
    }

    //设置停止位
    switch(ui->stopBitsComboBox->currentIndex()){
    case 0:
        myCom->setStopBits(STOP_1);
        break;
    case 1:
 #ifdef Q_OS_WIN
        myCom->setStopBits(STOP_1_5);
 #endif
        break;
    case 2:
        myCom->setStopBits(STOP_2);
        break;
    default:
        myCom->setStopBits(STOP_1);
        qDebug("set to default : STOP_1");
        break;
    }    

    //设置数据流控制
    myCom->setFlowControl(FLOW_OFF);
    //设置延时
    myCom->setTimeout(TIME_OUT);

	if (myCom->open(QIODevice::ReadWrite))
	{
//		QMessageBox::information(this, tr("打开成功"), tr("已成功打开串口") + portName, QMessageBox::Ok);
		//界面控制
		ui->sendmsgBtn->setEnabled(true);
        setComboBoxEnabled(false);

        ui->sendMsgLineEdit->setEnabled(true);

        ui->actionOpen->setEnabled(false);
        ui->sendMsgLineEdit->setFocus();
        ui->obocheckBox->setEnabled(true);
        ui->actionAdd->setEnabled(false);

		setActionsEnabled(true);
	}
	else
	{
		QMessageBox::critical(this, tr("打开失败"), tr("未能打开串口 ") + portName + tr("\n该串口设备不存在或已被占用"), QMessageBox::Ok);
		return;
	}

    ui->statusBar->showMessage(tr("打开串口成功"));
}


//关闭串口
void MainWindow::on_actionClose_triggered()
{

    myCom->close();
    delete myCom;
    myCom = NULL;

    ui->sendmsgBtn->setEnabled(false);

    setComboBoxEnabled(true);

    ui->actionOpen->setEnabled(true);
    ui->sendMsgLineEdit->setEnabled(false);
    ui->obocheckBox->setEnabled(false);
    ui->actionAdd->setEnabled(true);

    setActionsEnabled(false);

    ui->actionWriteToFile->setChecked(false);
    ui->statusBar->showMessage(tr("串口已经关闭"));

    m_pCircleSendTimer->stop();
    ui->msg->setText("停止循环发送.");
}

//关于
void MainWindow::on_actionAbout_triggered()
{
    aboutdlg.show();
    // 在主窗口中间显示
    int x =this->x() + (this->width() - aboutdlg.width()) / 2;
    int y =this->y() + (this->height() - aboutdlg.height()) / 2;
    aboutdlg.move(x, y);
    ui->statusBar->showMessage(tr("关于Wincom"));
}

//读取数据
void MainWindow::readMyCom()
{
    QByteArray temp = myCom->readAll();
    QString buf;

    if(!temp.isEmpty()){
            ui->textBrowser->setTextColor(Qt::black);
            if(ui->ccradioButton->isChecked()){
                buf = temp;
            }else if(ui->chradioButton->isChecked()){
                QString str;
                for(int i = 0; i < temp.count(); i++){
                    QString s;
//                    if ( ui->chexButton->isChecked() )
//                        s.sprintf("0x%02x, ", (unsigned char)temp.at(i));
//                    else
                    s.sprintf("%02x ", (unsigned char)temp.at(i));
                    buf += s;
                }
            }

        if(!write2fileName.isEmpty()){
            QFile file(write2fileName);
            //如果打开失败则给出提示并退出函数
            if(!file.open(QFile::WriteOnly | QIODevice::Text)){
                QMessageBox::warning(this, tr("写入文件"), tr("打开文件 %1 失败, 无法写入\n%2").arg(write2fileName).arg(file.errorString()), QMessageBox::Ok);
                return;
            }
            QTextStream out(&file);
            out<<buf;
            file.close();
        }

        QString timeStrLine="["+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+"][接收]: ";
        buf = timeStrLine+buf+"\n\r";

        QString content = "<span style=\" color:blue;\">"+buf+"</span>";
//        ui->textBrowser->setText(ui->textBrowser->document()->toPlainText() + content);
        ui->textBrowser->append(content);
//        QTextCursor cursor = ui->textBrowser->textCursor();
//        cursor.movePosition(QTextCursor::End);
//        ui->textBrowser->setTextCursor(cursor);

        ui->recvbyteslcdNumber->display(ui->recvbyteslcdNumber->value() + temp.size());
        ui->statusBar->showMessage(tr("成功读取%1字节数据").arg(temp.size()));
    }
}

//发送数据
void MainWindow::sendMsg()
{
    QByteArray buf;
    QString msg;

    if(ui->sendAsHexcheckBox->isChecked()){
        QString str;
        bool ok;
        char data;
        QStringList list;
        str = ui->sendMsgLineEdit->text();
        int pos = str.indexOf(" ");
        if (pos > 1)
        {
            list = str.split(" ");
            for(int i = 0; i < list.count(); i++){
                if(list.at(i) == " ")
                    continue;
                if(list.at(i).isEmpty())
                    continue;
                data = (char)list.at(i).toInt(&ok, 16);
                if(!ok){
                    QMessageBox::information(this, tr("提示消息"), tr("输入的数据格式有错误！"), QMessageBox::Ok);
                    if(obotimer != NULL)
                        obotimer->stop();
                    ui->sendmsgBtn->setText(tr("发送"));
                    ui->sendmsgBtn->setIcon(QIcon(":://images/send.png"));
                    return;


                }
                buf.append(data);
            }

            msg = str;
        }
        else
        {
            buf = QByteArray::fromHex(str.toLatin1());
            QByteArray tmp, src;
            src = str.toLatin1();
            for (int i = 0; i < src.size(); i++ )
            {
                tmp.append(src.at(i));
                if (i % 2) tmp.append(0x20);
            }
            msg = tmp;
        }
    }else{
#if QT_VERSION < 0x050000
        buf = ui->sendMsgLineEdit->text().toAscii();
#else
        buf = ui->sendMsgLineEdit->text().toLocal8Bit();
#endif
        msg = buf;
    }


    QString timeStrLine="["+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+"][发送]: ";

    QString content = "<span style=\" color:red;\">"+timeStrLine +msg+"\n\r</span>";
//    ui->textBrowser->setText(ui->textBrowser->document()->toPlainText() + content);
    ui->textBrowser->append(content);
//    QTextCursor cursor = ui->textBrowser->textCursor();
//    cursor.movePosition(QTextCursor::End);
//    ui->textBrowser->setTextCursor(cursor);

    //发送数据
    myCom->write(buf);
    ui->statusBar->showMessage(tr("发送数据成功"));
    //界面控制
    ui->textBrowser->setTextColor(Qt::lightGray);
}

//发送数据按钮
void MainWindow::on_sendmsgBtn_clicked()
{
    //如果当前正在连续发送数据，暂停发送
    if(ui->sendmsgBtn->text() == tr("暂停")){
        obotimer->stop();
        ui->sendmsgBtn->setText(tr("发送"));
        ui->sendmsgBtn->setIcon(QIcon("://images/send.png"));
        return;
    }
    //如果发送数据为空，给出提示并返回
    if(ui->sendMsgLineEdit->text().isEmpty()){
        QMessageBox::information(this, tr("提示消息"), tr("没有需要发送的数据"), QMessageBox::Ok);
        return;
    }

    //如果不是连续发送
    if(!ui->obocheckBox->isChecked()){
        ui->sendMsgLineEdit->setFocus();
        //发送数据
        sendMsg();
    }else{ //连续发送
        obotimer->start(obotimerdly);
        ui->sendmsgBtn->setText(tr("暂停"));
        ui->sendmsgBtn->setIcon(QIcon("://images/pause.png"));
    }
}
//清空记录
void MainWindow::on_clearUpBtn_clicked()
{
    ui->textBrowser->clear();
    ui->statusBar->showMessage(tr("记录已经清空"));
}



//计数器清零
void MainWindow::on_actionClearBytes_triggered()
{
    if(ui->recvbyteslcdNumber->value() == 0){
        QMessageBox::information(this, tr("提示消息"), tr("貌似已经清零了呀:)"), QMessageBox::Ok);
    }else{
        ui->recvbyteslcdNumber->display(0);
        ui->statusBar->showMessage(tr("计数器已经清零"));
    }
}

//单击连续发送checkBox
void MainWindow::on_obocheckBox_clicked()
{
    if(ui->obocheckBox->isChecked()){
        ui->delayspinBox->setEnabled(true);
        //检查是否有数据，如有则启动定时器
        ui->statusBar->showMessage(tr("启用连续发送"));
    }else{
        ui->delayspinBox->setEnabled(false);
        //若定时器已经启动则关闭它
        ui->statusBar->showMessage(tr("停止连续发送"));
    }
}

//保存textBrowser中的内容
void MainWindow::on_actionSave_triggered()
{

    if(ui->textBrowser->toPlainText().isEmpty()){
        QMessageBox::information(this, "提示消息", tr("貌似还没有数据! 您需要在发送编辑框中输入要发送的数据"), QMessageBox::Ok);
        return;
    }

    QString filename = QFileDialog::getSaveFileName(this, tr("保存为"), tr("未命名.txt"));
    QFile file(filename);
    //如果用户取消了保存则直接退出函数
    if(file.fileName().isEmpty()){
        return;
    }
    //如果打开失败则给出提示并退出函数
    if(!file.open(QFile::WriteOnly | QIODevice::Text)){
        QMessageBox::warning(this, tr("保存文件"), tr("打开文件 %1 失败, 无法保存\n%2").arg(filename).arg(file.errorString()), QMessageBox::Ok);
        return;
    }
    //写数据到文件
    QTextStream out(&file);
    out<<ui->textBrowser->toPlainText();
    file.close();
    //修改窗口标题为保存文件路径
//    setWindowTitle("saved: " + QFileInfo(filename).canonicalFilePath());

}

//退出程序
void MainWindow::on_actionExit_triggered()
{
    this->close();
}

//添加串口
void MainWindow::on_actionAdd_triggered()
{
    bool ok = false;
    QString portname;

    portname = QInputDialog::getText(this, tr("添加串口"), tr("设备文件名"), QLineEdit::Normal, 0, &ok);
    if(ok && !portname.isEmpty()){
        ui->portNameComboBox->addItem(portname);
        ui->statusBar->showMessage(tr("添加串口成功"));
    }
}

//调整连续发送时间间隔
void MainWindow::on_delayspinBox_valueChanged(int )
{
    obotimerdly = ui->delayspinBox->value();
}

//载入外部文件
void MainWindow::on_actionLoadfile_triggered()
{
    QString filename = QFileDialog::getOpenFileName(this, tr("读取文件"), 0, tr("保存为 (*.txt *.log)"));  //添加更多的文件类型
    QFile file(filename);

    //如果取消打开则退出函数
    if(file.fileName().isEmpty()){
        return;
    }

    //如果打开失败则提示并退出函数
    if(!file.open(QFile::ReadOnly | QIODevice::Text)){
        QMessageBox::warning(this, tr("打开失败"),
                             tr("抱歉! Wincom未能打开此文件, 这可能是由于没有足够的权限造成的. 您可以尝试使用chmod命令修改文件权限."),
                             QMessageBox::Ok);
        return;
    }

    //文件大小超过限制
    if(file.size() > MAX_FILE_SIZE){
        QMessageBox::critical(this, tr("载入失败"), tr("文件大小为 %1 字节, 超过限制大小 10000 字节").arg(file.size()), QMessageBox::Ok);
        return;
    }
    //文件太大时提示是否继续打开
    if(file.size() > TIP_FILE_SIZE){
        if(QMessageBox::question(this, tr("提示消息"), tr("您要打开的文件过大, 这将消耗更多一些的时间,要继续打开么?"),
                                 QMessageBox::Yes | QMessageBox::No) == QMessageBox::No){

            return;
        }
    }

    //读取数据并加入到发送数据编辑框
    QByteArray line;
    while(!file.atEnd()){
        line = file.readLine();
        ui->sendMsgLineEdit->setText(ui->sendMsgLineEdit->text() + tr(line));

    }
    file.close();
    ui->statusBar->showMessage(tr("已经成功读取文件中的数据"));
}

//清空串口中的I/O数据
void MainWindow::on_actionCleanPort_triggered()
{
    myCom->flush();
}

//写入文件菜单
void MainWindow::on_actionWriteToFile_triggered()
{
    if(ui->actionWriteToFile->isChecked()){
        QString filename = QFileDialog::getSaveFileName(this, tr("写入文件"), 0, tr("保存为 (*.*)"));  //添加更多的文件类型
        if(filename.isEmpty()){
            ui->actionWriteToFile->setChecked(false);
        }else{
            write2fileName = filename;
            //ui->textBrowser->setEnabled(false);
            ui->actionWriteToFile->setToolTip(tr("停止写入到文件"));
        }
    }else{
        write2fileName.clear();
        //ui->textBrowser->setEnabled(true);
        ui->actionWriteToFile->setToolTip(tr("将读取数据写入到文件"));
    }

}

void MainWindow::SendMultiMsg(int id)
{
    QByteArray buf;
    QString msg;

    QLineEdit* txt = ui->tabWidget->findChild<QLineEdit*>(QString("txtSendData%1")
        .arg(QString::number(id)));

    QString str = txt->text();
    if (str.isNull() || str.isEmpty())
    {
        ui->msg->setText(QString("%1: 没有发送内容!").arg(id));
        return;
    }

    // HEX方式发送
    if (ui->cbMSHex->isChecked())
    {
        bool ok;
        char data;
        QStringList list;
        if ( str.indexOf(" ") > 0)
        {
            list = str.split(" ");
            for (int i = 0; i < list.count(); i++)
            {
                if (list.at(i) == " ")
                    continue;
                if (list.at(i).isEmpty())
                    continue;
                data = (char)list.at(i).toInt(&ok, 16);
                if (!ok)
                {
                    QMessageBox::information(this, tr("提示消息"), tr("输入的数据格式有错误！"), QMessageBox::Ok);
                    if (obotimer != NULL)
                        obotimer->stop();
                    return;
                }
                buf.append(data);
            }
             msg = str;
        }
        else
        {
            buf = QByteArray::fromHex(str.toLatin1());
            QByteArray tmp, src;
            src = str.toLatin1();
            for (int i = 0; i < src.size(); i++ )
            {
                tmp.append(src.at(i));
                if (i % 2) tmp.append(0x20);
            }
            msg = tmp;
        }
    }
    else
    {
#if QT_VERSION < 0x050000
        buf = txt->text().toAscii();
#else
        buf = txt->text().toLocal8Bit();
#endif
        msg = buf;
    }
    //发送数据
    if (myCom && myCom->isOpen() && myCom->isWritable())
    {
        qDebug()<<id<<"**************************buf : "<<buf.toHex();
        QString timeStrLine="["+QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")+"][发送]: ";
        QString content = "<span style=\" color:red;\">"+timeStrLine +msg+"\n\r</span>";
        ui->textBrowser->append(content);

        myCom->write(buf);
        ui->statusBar->showMessage(tr("发送数据成功"));
        //界面控制
        ui->textBrowser->setTextColor(Qt::red);
    }
    else
    {
        if (QMessageBox::Ok ==
            QMessageBox::question(this, tr("提示"),
                QString(tr("串口 %1 并没有打开, 是否要打开串口 %2 ?"))
                .arg(ui->portNameComboBox->currentText(), ui->portNameComboBox->currentText()),
                QMessageBox::Ok, QMessageBox::Cancel))
        {
            on_actionOpen_triggered();
        }
    }
}

//分组发送按键槽函数
void MainWindow::SendMultiData()
{
    if (QPushButton* btn = dynamic_cast<QPushButton*>(sender()))
    {
        // 判断如果Sender是QPushButton就执行
        // send_key(btn->whatsThis());
        QString szObjName = btn->objectName();
        int nBtnID = szObjName.replace("pushBtnSendData", "").toInt();
        // 合法的ID
        if (nBtnID > 0 && nBtnID < 34)
        {
            SendMultiMsg(nBtnID);
        }
    }
}

void MainWindow::on_cbAutoForSend_clicked(bool checked)
{
    if (checked)
    {
        if (myCom && myCom->isOpen() && myCom->isWritable())
        {
            ui->msg->setText("启动循环发送.");
            m_pCircleSendTimer->setInterval(ui->txtSendDelay->text().toInt());
            m_pCircleSendTimer->start();
        }
        else
        {
            if (QMessageBox::Ok ==
                QMessageBox::question(this, tr("提示"),
                    QString(tr("串口 %1 并没有打开, 是否要打开串口 %2 ?"))
                    .arg(ui->portNameComboBox->currentText(), ui->portNameComboBox->currentText()),
                    QMessageBox::Ok, QMessageBox::Cancel))
            {
                on_actionOpen_triggered();
            }
            ui->cbAutoForSend->setChecked(false);
        }
    }
    else
    {
        m_pCircleSendTimer->stop();
        ui->msg->setText("停止循环发送.");
    }

}

//循环发送
void MainWindow::onCircleSend()
{
    QCheckBox* cbSend;

    while (true)
    {
        cbSend = ui->tabWidget->findChild<QCheckBox*>(QString("cb%1")
            .arg(QString::number(m_curIndexId)));

        if (cbSend->isChecked())
        {
            SendMultiMsg(m_curIndexId);
            m_curIndexId++;
            break;
        }
        else
        {
            m_curIndexId++;
            if ( m_curIndexId >= 34 )
            {
                m_curIndexId = 1;
            }
        }
    }

}
